package branice.spring.cloud.controller;


import branice.spring.cloud.result.CommonResult;
import branice.spring.cloud.result.enums.ResultCodeEnum;
import branice.spring.cloud.service.OpenFeignService;
import branice.spring.cloud.util.IdWorker;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/eurekaSever")
@RestController
public class OpenFeignController extends IdWorker {

    private static final Logger log = LoggerFactory.getLogger(OpenFeignController.class);

    @Autowired
    OpenFeignService openFeignService;


    /**
     * @param body
     * @return
     */
    @PostMapping("/sendReq")
    public CommonResult sendRequest(@RequestBody String body) {
        String logId = nextId() + "";
        long begin = System.currentTimeMillis();
        CommonResult result = null;
        log.info("sendReq-{}-req-执行开始, rowRequest={}", logId, body);

        try {

            result = new CommonResult(ResultCodeEnum.SUCCESS, openFeignService.sendReq(logId, body));
            log.info("sendReq-{}-resp-执行完成, rowRequest={}, result={}, 共耗时{}ms", logId, body, JSONObject.toJSON(result), System.currentTimeMillis() - begin);

        } catch (Exception e) {
            log.error("sendReq-{}-req-执行异常, rowRequest={}, e=", logId, body, e);
        }

        return result;
    }


}
